package com.lb.myeam.common.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.lb.myeam.common.consts.SysConst;

public class DBUtil {
	private static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
	public static Connection getConn(String driverClass,String jdbcURL,String dba,String pwd){
		try {
			Class.forName(driverClass);
			return DriverManager.getConnection(jdbcURL,dba,pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Connection getOracleConn(String jdbcURL,String dba,String pwd){
		return getConn(ORACLE_DRIVER, jdbcURL, dba, pwd);
	}
	
	public static Connection getConn(){
		Properties props = PropertiesUtil.load(SysConst.DB_CONFIG_FILE);
		String url = props.getProperty("jdbc_url");
		String dba = props.getProperty("jdbc_dba");
		String pwd = props.getProperty("jdbc_pwd");
		String driverClass = props.getProperty("jdbc_driver_class");
		return getConn(driverClass, url, dba, pwd);
	}
	
	public static void close(ResultSet rs,Statement stmt,Connection conn){
		if (rs != null) {
			try {
				rs.close();
				rs = null;
			} catch (SQLException e) {
				
			}
		}
		if (conn != null) {
			try {
				stmt.close();
				stmt = null;
			} catch (SQLException e) {
			}
		}
		if (conn != null) {
			try {
				conn.close();
				conn = null;
			} catch (SQLException e) {
			}
		}
	}
}
